using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class EmojiTextNotCut : Panel
	{
		private const string TAG = "EmojiTextNotCut";
		public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(EmojiTextNotCut), new PropertyMetadata(null, new PropertyChangedCallback(EmojiTextNotCut.OnTextChanged)));
		public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register("FontSize", typeof(double), typeof(EmojiTextNotCut), new PropertyMetadata(18.0, new PropertyChangedCallback(EmojiTextNotCut.OnFontSizeChanged)));
		public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register("Foreground", typeof(Brush), typeof(EmojiTextNotCut), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)), new PropertyChangedCallback(EmojiTextNotCut.OnBrushChanged)));
		public static readonly DependencyProperty EmojiSizeProperty = DependencyProperty.Register("EmojiSize", typeof(double), typeof(EmojiTextNotCut), new PropertyMetadata(18.0, new PropertyChangedCallback(EmojiTextNotCut.OnEmojiSizeChanged)));
		public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register("FontFamily", typeof(FontFamily), typeof(EmojiTextNotCut), new PropertyMetadata(new FontFamily("{StaticResource PhoneFontFamilySemiLight}"), new PropertyChangedCallback(EmojiTextNotCut.OnFontFamilyChanged)));
		public bool IsTextTooLong;
		private string lastStr;
		public string Text
		{
			get
			{
				return (string)base.GetValue(EmojiTextNotCut.TextProperty);
			}
			set
			{
				base.SetValue(EmojiTextNotCut.TextProperty, value);
			}
		}
		public double FontSize
		{
			get
			{
				return (double)base.GetValue(EmojiTextNotCut.FontSizeProperty);
			}
			set
			{
				base.SetValue(EmojiTextNotCut.FontSizeProperty, value);
			}
		}
		public Brush Foreground
		{
			get
			{
				return (Brush)base.GetValue(EmojiTextNotCut.ForegroundProperty);
			}
			set
			{
				base.SetValue(EmojiTextNotCut.ForegroundProperty, value);
			}
		}
		public double EmojiSize
		{
			get
			{
				return (double)base.GetValue(EmojiTextNotCut.EmojiSizeProperty);
			}
			set
			{
				base.SetValue(EmojiTextNotCut.EmojiSizeProperty, value);
			}
		}
		public FontFamily FontFamily
		{
			get
			{
				return (FontFamily)base.GetValue(EmojiTextNotCut.FontFamilyProperty);
			}
			set
			{
				base.SetValue(EmojiTextNotCut.FontFamilyProperty, value);
			}
		}
		public EmojiTextNotCut()
		{
			base.set_MaxHeight(1000.0);
		}
		private static void OnTextChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((EmojiTextNotCut)dependencyObject_0).OnTextChanged(dependencyPropertyChangedEventArgs_0);
		}
		protected virtual void OnTextChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			this.RefreshTxt();
			base.InvalidateMeasure();
		}
		private static void OnFontSizeChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((EmojiTextNotCut)dependencyObject_0).OnFontSizeChanged(dependencyPropertyChangedEventArgs_0);
		}
		protected virtual void OnFontSizeChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (this.FontSize > 32.0)
			{
				this.EmojiSize = 32.0;
			}
			else
			{
				this.EmojiSize = this.FontSize;
			}
			base.InvalidateMeasure();
		}
		private static void OnBrushChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((EmojiTextNotCut)dependencyObject_0).OnBrushChanged(dependencyPropertyChangedEventArgs_0);
		}
		protected virtual void OnBrushChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			base.InvalidateMeasure();
		}
		private static void OnEmojiSizeChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((EmojiTextNotCut)dependencyObject_0).OnEmojiSizeChanged(dependencyPropertyChangedEventArgs_0);
		}
		protected virtual void OnEmojiSizeChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			base.InvalidateMeasure();
		}
		private static void OnFontFamilyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((EmojiTextNotCut)dependencyObject_0).OnFontFamilyChanged(dependencyPropertyChangedEventArgs_0);
		}
		protected virtual void OnFontFamilyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			base.InvalidateMeasure();
		}
		protected override Size MeasureOverride(Size availableSize)
		{
			base.MeasureOverride(availableSize);
			if (double.IsInfinity(availableSize.get_Width()) || double.IsInfinity(availableSize.get_Height()))
			{
				availableSize.set_Width(2000.0);
				availableSize.set_Height(2000.0);
			}
			double num = 0.0;
			double num2 = availableSize.get_Width();
			double height = availableSize.get_Height();
			for (int i = 0; i < base.get_Children().get_Count(); i++)
			{
				UIElement uIElement = base.get_Children().get_Item(i);
				if (uIElement is TextBlock)
				{
					uIElement.Measure(new Size(num2, height));
				}
				else
				{
					uIElement.Measure(new Size(num2, height));
				}
				num2 -= uIElement.get_DesiredSize().get_Width();
				if (num2 < 0.0)
				{
					num2 = 0.0;
				}
				num += uIElement.get_DesiredSize().get_Width();
			}
			double num3 = this.FontSize * 1.3;
			return new Size(num, num3);
		}
		protected override Size ArrangeOverride(Size finalSize)
		{
			if (base.get_Children().get_Count() <= 0)
			{
				return base.ArrangeOverride(finalSize);
			}
			double num = 0.0;
			double num2 = finalSize.get_Width();
			double height = finalSize.get_Height();
			bool flag = false;
			for (int i = 0; i < base.get_Children().get_Count(); i++)
			{
				UIElement uIElement = base.get_Children().get_Item(i);
				double width = uIElement.get_DesiredSize().get_Width();
				double arg_6B_0 = uIElement.get_DesiredSize().get_Height();
				if (base.get_Children().get_Item(i) is Image && num2 < this.EmojiSize)
				{
					flag = true;
				}
				TextBlock arg_9C_0 = base.get_Children().get_Item(i) as TextBlock;
				if (flag)
				{
					base.get_Children().get_Item(i).Arrange(new Rect(0.0, 0.0, 0.0, 0.0));
				}
				else
				{
					base.get_Children().get_Item(i).Arrange(new Rect(num, 0.0, width, height));
				}
				if (uIElement != null && num2 <= uIElement.get_DesiredSize().get_Width())
				{
					flag = true;
				}
				num += width;
				num2 -= width;
			}
			this.IsTextTooLong = flag;
			return new Size(num, height);
		}
		private void genImage(string imgSrc)
		{
			if (string.IsNullOrEmpty(imgSrc))
			{
				return;
			}
			Image image = new Image();
			image.set_Source(new ImageSourceConverter().ConvertFromString(imgSrc) as ImageSource);
			Binding binding = new Binding("EmojiSize");
			binding.set_Source(this);
			image.SetBinding(FrameworkElement.WidthProperty, binding);
			Binding binding2 = new Binding("EmojiSize");
			binding2.set_Source(this);
			image.SetBinding(FrameworkElement.HeightProperty, binding2);
			base.get_Children().Add(image);
		}
		private bool genTextBlock(ref string tempStr)
		{
			if (string.IsNullOrEmpty(tempStr))
			{
				return false;
			}
			bool result = false;
			int num = tempStr.IndexOf('\n');
			int num2 = tempStr.IndexOf('\r');
			if (num >= 0 && num2 >= 0)
			{
				num = Math.Min(num, num2);
			}
			else
			{
				num = Math.Max(num, num2);
			}
			if (num > 0)
			{
				tempStr = tempStr.Substring(0, num);
				result = true;
			}
			else
			{
				if (num == 0)
				{
					tempStr = "";
					return true;
				}
			}
			TextBlock textblock = this.getTextblock();
			textblock.set_Text(tempStr);
			Binding binding = new Binding("FontSize");
			binding.set_Source(this);
			textblock.SetBinding(TextBlock.FontSizeProperty, binding);
			Binding binding2 = new Binding("Foreground");
			binding2.set_Source(this);
			textblock.SetBinding(TextBlock.ForegroundProperty, binding2);
			Binding binding3 = new Binding("FontFamily");
			binding3.set_Source(this);
			textblock.SetBinding(TextBlock.FontFamilyProperty, binding3);
			base.get_Children().Add(textblock);
			tempStr = "";
			return result;
		}
		private TextBlock getTextblock()
		{
			TextBlock textBlock = new TextBlock();
			textBlock.set_TextWrapping(2);
			textBlock.set_LineHeight(this.FontSize * 2.0);
			return textBlock;
		}
		private void RefreshTxt()
		{
			if (this.lastStr == this.Text)
			{
				return;
			}
			this.lastStr = this.Text;
			if (string.IsNullOrEmpty(this.Text))
			{
				base.get_Children().Clear();
				return;
			}
			base.get_Children().Clear();
			string text = "";
			for (int i = 0; i < this.Text.get_Length(); i++)
			{
				int emojiPos = EmojiManager.GetInstance().getEmojiPos(this.Text.get_Chars(i));
				if (emojiPos != -1)
				{
					if (this.genTextBlock(ref text))
					{
						return;
					}
					string emojiSrcFromFilePos = EmojiManager.GetInstance().getEmojiSrcFromFilePos(emojiPos);
					if (emojiSrcFromFilePos != null)
					{
						this.genImage(emojiSrcFromFilePos);
					}
					else
					{
						Log.smethod_1("EmojiTextNotCut", string.Concat(new object[]
						{
							"imgSrc == null,pos:",
							emojiPos,
							",Text[",
							i,
							"]:",
							string.Format("{0:X0000}", (int)this.Text.get_Chars(i))
						}));
					}
				}
				else
				{
					text += this.Text.get_Chars(i);
				}
			}
			this.genTextBlock(ref text);
		}
	}
}
